<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="工单编号" prop="workorderCode">
        <el-input
          v-model="queryParams.workorderCode"
          placeholder="请输入工单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品编号" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="停产时间" prop="stopTime">
        <el-date-picker
          clearable
          v-model="queryParams.stopTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择停产时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="恢复时间" prop="resumeTime">
        <el-date-picker
          clearable
          v-model="queryParams.resumeTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择恢复时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['pro:records:add']"-->
      <!--        >新增</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['pro:records:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['pro:records:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['produce:stopProd:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordsList" @selection-change="handleSelectionChange"  height="calc(100vh - 280px)">
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="id" align="center" prop="id" />-->
      <!--      <el-table-column label="工单id" align="center" prop="workorderId" />-->
      <el-table-column label="序号" align="center" width="50px">
        <template slot-scope="scope">
          <span>{{scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="工单编号" align="center" prop="workorderCode" width="110px" />
      <el-table-column label="类型" align="center" prop="orderType" width="90px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_order_type" :value="scope.row.orderType" />
        </template>
      </el-table-column>
      <!--      <el-table-column label="产品id" align="center" prop="productId" />-->
      <el-table-column label="产品编号" align="center" prop="productCode" width="110px" />
      <el-table-column label="产品名称" align="center" prop="productName" width="130px" />
      <el-table-column label="产品规格" align="center" prop="productSpec" width="220px" />
      <!--      <el-table-column label="产品单位" align="center" prop="unit" />-->

      <el-table-column label="异常类型" align="center" prop="exceptionType" width="100px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_exception_type" :value="scope.row.exceptionType" />
        </template>
      </el-table-column>

      <el-table-column label="异常描述" align="center" prop="exceptionDescription" />
      <!--      <el-table-column label="异常时间" align="center" prop="exceptionTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.exceptionTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="停产时间" align="center" prop="stopTime" width="180px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stopTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="恢复时间" align="center" prop="resumeTime" width="180px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.resumeTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时长" align="center" prop="a" width="80px">
        <template slot-scope="scope">
          <span>{{ timeMoment(scope.row) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="恢复人" align="center" prop="resumeBy" width="80px" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_records_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-edit"-->
      <!--            @click="handleUpdate(scope.row)"-->
      <!--            v-hasPermi="['pro:records:edit']"-->
      <!--          >修改</el-button>-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-delete"-->
      <!--            @click="handleDelete(scope.row)"-->
      <!--            v-hasPermi="['pro:records:remove']"-->
      <!--          >删除</el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工单停产记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工单id" prop="workorderId">
          <el-input v-model="form.workorderId" placeholder="请输入工单id" />
        </el-form-item>
        <el-form-item label="工单编号" prop="workorderCode">
          <el-input v-model="form.workorderCode" placeholder="请输入工单编号" />
        </el-form-item>
        <el-form-item label="产品id" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入产品id" />
        </el-form-item>
        <el-form-item label="产品编号" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入产品编号" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="产品规格" prop="productSpec">
          <el-input v-model="form.productSpec" placeholder="请输入产品规格" />
        </el-form-item>
        <el-form-item label="产品单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入产品单位" />
        </el-form-item>
        <el-form-item label="停产时间" prop="stopTime">
          <el-date-picker
            clearable
            v-model="form.stopTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择停产时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="恢复时间" prop="resumeTime">
          <el-date-picker
            clearable
            v-model="form.resumeTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择恢复时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="恢复人" prop="resumeBy">
          <el-input v-model="form.resumeBy" placeholder="请输入恢复人" />
        </el-form-item>
        <el-form-item label="异常描述信息" prop="exceptionDescription">
          <el-input v-model="form.exceptionDescription" placeholder="请输入异常描述信息" />
        </el-form-item>
        <el-form-item label="异常时间" prop="exceptionTime">
          <el-date-picker
            clearable
            v-model="form.exceptionTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择异常时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRecords,
  getRecords,
  delRecords,
  addRecords,
  updateRecords
} from "@/api/mes/pro/records";
import  moment from "moment";

export default {
  name: "Records",
  dicts: [
    "mes_workorder_status",
    "mes_order_type",
    "mes_exception_type",
    "mes_records_status"
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工单停产记录表格数据
      recordsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workorderId: null,
        workorderCode: null,
        orderType: null,
        productId: null,
        productCode: null,
        productName: null,
        productSpec: null,
        unit: null,
        stopTime: null,
        resumeTime: null,
        resumeBy: null,
        exceptionType: null,
        exceptionDescription: null,
        exceptionTime: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workorderId: [
          { required: true, message: "工单id不能为空", trigger: "blur" }
        ],
        workorderCode: [
          { required: true, message: "工单编号不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工单停产记录列表 */
    getList() {
      this.loading = true;
      listRecords(this.queryParams).then(response => {
        this.recordsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        workorderId: null,
        workorderCode: null,
        orderType: null,
        productId: null,
        productCode: null,
        productName: null,
        productSpec: null,
        unit: null,
        stopTime: null,
        resumeTime: null,
        resumeBy: null,
        exceptionType: null,
        exceptionDescription: null,
        exceptionTime: null,
        status: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.queryParams.proWorkorderStopproductRecordsIdList = selection.map(
        item => item.id
      );
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工单停产记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getRecords(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工单停产记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecords(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecords(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除工单停产记录编号为"' + ids + '"的数据项？')
        .then(function() {
          return delRecords(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "mes/pro/records/export",
        {
          ...this.queryParams
        },
        `records_${new Date().getTime()}.xlsx`
      );
    },
    timeMoment(row) {
      let str = this.timeDiff(row.stopTime,row.resumeTime)
      return str
    }
  }
};
</script>

